package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.master.EntityPayChannel;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.web.DelPayChannelRequest;
import com.ocom.common.request.web.EditPayChannelRequest;
import com.ocom.common.request.web.GetPayChannelRequest;
import com.ocom.common.request.web.SavePayChannelRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoPayChannel;
import com.ocom.payconfig.service.PayChannelService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class PayChannelServiceImpl extends ServiceImpl<IDaoPayChannel, EntityPayChannel> implements PayChannelService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public Result getPayChannelList(GetPayChannelRequest request) {
		QueryWrapper<EntityPayChannel> queryWrapper=new QueryWrapper<>();
		if(request.getId()>0){
			queryWrapper.eq("id",request.getId());
		}
		if(!CommonUtil.isNull(request.getCode())){
			queryWrapper.eq("code",request.getCode());
		}
		if(!CommonUtil.isNull(request.getName())){
			queryWrapper.eq("name",request.getName());
		}
		//多条
		if(request.getSearch()==0){
			PageHelper.startPage(0,100);
			List<EntityPayChannel> pointInfoPageInfo=this.list(queryWrapper);
			PageInfo pageInfo=new PageInfo<>(pointInfoPageInfo);
			return ResultUtil.success(pageInfo);
		}else{
			queryWrapper.last("limit 1");
			EntityPayChannel entityPayChannel=this.getOne(queryWrapper);
			return ResultUtil.success(entityPayChannel);
		}
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityPayChannel addPageChannel(SavePayChannelRequest request) {
		EntityPayChannel entityPayChannel=new EntityPayChannel();
		entityPayChannel.setPayWay(request.getCode());
		entityPayChannel.setName(request.getName());
		entityPayChannel.setParams(request.getParams());
		entityPayChannel.setUrl("test");
		entityPayChannel.setKeyTip(request.getKey());
		entityPayChannel.setRemark(request.getRemark());
		entityPayChannel.setCreateTime(new Date());
		boolean b=this.save(entityPayChannel);
		if(b){
			return entityPayChannel;
		}
		return null;
	}


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityPayChannel editPayChannel(EditPayChannelRequest request) {
		QueryWrapper<EntityPayChannel> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("id",request.getId());
		EntityPayChannel entityPayChannel=this.getOne(queryWrapper);
		if(entityPayChannel==null){
			return null;
		}
		entityPayChannel.setPayWay(request.getCode());
		entityPayChannel.setName(request.getName());
		entityPayChannel.setParams(request.getParams());
		entityPayChannel.setRemark(request.getRemark());
		entityPayChannel.setUrl("test");
		entityPayChannel.setKeyTip(request.getKey());
		entityPayChannel.setUpdateTime(new Date());
		boolean b=this.updateById(entityPayChannel);
		if(b){
			return entityPayChannel;
		}
		return null;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
	public boolean delPageChannel(DelPayChannelRequest request) {
		QueryWrapper<EntityPayChannel> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("id",request.getId());
		return  this.remove(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public EntityPayChannel getPayTip(Integer payWay) {
		QueryWrapper<EntityPayChannel> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("id",payWay);
		EntityPayChannel entityPayChannel=this.getOne(queryWrapper);
		if(entityPayChannel==null){
			return null;
		}
		return entityPayChannel;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public EntityPayChannel getPayTipByCode(Integer payWay) {
		QueryWrapper<EntityPayChannel> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("pay_way",payWay);
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public List<EntityPayChannel> payList() {
		return this.list();
	}

}
